<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>新增学生</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="">
			<div class="mainBox">
				<div class="main-container">
					<div class="layui-form-item">
						<label class="layui-form-label">姓名</label>
						<div class="layui-input-block">
							<input type="text" name="sname" lay-verify="sname" autocomplete="off" placeholder="请输入姓名"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">QQ</label>
						<div class="layui-input-block">
							<input type="text" name="QQ" lay-verify="QQ" autocomplete="off" placeholder="请输入QQ号"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">微信号</label>
						<div class="layui-input-block">
							<input type="text" name="wechat" lay-verify="wechat" autocomplete="off" placeholder="请输入微信号"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">手机号</label>
						<div class="layui-input-block">
							<input type="text" name="telephone" lay-verify="telephone" autocomplete="off"
								placeholder="请输入微信号" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">密码</label>
						<div class="layui-input-block">
							<input type="text" name="password" lay-verify="password" required autocomplete="off"
								placeholder="请输入标题" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">入学时间</label>
						<div class="layui-input-block">
							<input type="date" id="enerdate" name="enerdate" lay-verify="enerdate" autocomplete="off"
								placeholder="入学时间" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">出生日期</label>
						<div class="layui-input-block">
							<input type="date" id="birthday" name="birthday" lay-verify="birthday" autocomplete="off"
								placeholder="出生日期" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">性别</label>
						<div class="layui-input-block">
							<input type="radio" name="sex" value="男" title="男" checked>
							<input type="radio" name="sex" value="女" title="女"><input type="radio" name="sex" value="妖"
								title="妖">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">城市</label>
						<div class="layui-input-block">
							<input type="text" id="fromcity" name="fromcity" lay-verify="fromcity" autocomplete="off"
								placeholder="来自城市" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">地址</label>
						<div class="layui-input-block">
							<input type="text" name="address" lay-verify="address" autocomplete="off" placeholder="地址"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">年级</label>
						<div class="layui-input-block">
							<select name="gradeid" id="gradeid">
								<!-- <option value="1">高一</option> -->
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">身份证照片</label>
						<div class="layui-input-block">
							<input type="text" name="idcardpic" lay-verify="idcardpic" autocomplete="off"
								placeholder="照片" id="idcardpic" class="layui-input">
							<input class="btn-upload file-up" style="display: inline-block;" type="file"
								accept="image/*" name="idcardpicUpload" id="idcardpicUpload" value="上传">
						</div>
						<div class="layui-input-block">
							<img id="preview" height="200px" width="300px" src="#" alt="没有选择" />
							<span id="demoText"></span>
						</div>
					</div>
				</div>
			</div>

			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="user-save">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery', 'upload'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let upload = layui.upload;
				//上传图片
				var uploadInst = upload.render({
					elem: '#idcardpicUpload',
					url: window.BASEPATH + 'upload.do' //改成您自己的上传接口

						,
					auto: true //选择文件后不自动上传

						,
					choose: function(obj) {
						//预读本地文件示例，不支持ie8
						obj.preview(function(index, file, result) {
							$('#preview').attr('src', result); //图片链接（base64）
						});


					},
					before: function(obj) {
						layer.msg('上传中', {
							icon: 16,
							time: 0
						});
					},
					done: function(res) {
						//如果上传失败
						if (res.code > 0) {
							return layer.msg('上传失败');
						}
						$("#idcardpic").val(res.data);
						//上传成功的一些操作
						layer.msg("信息上传成功！", {
							time: 3000
						});
					},
					error: function() {
						//演示失败状态，并实现重传
						var demoText = $('#demoText');
						demoText.html(
							'<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>'
						);
						demoText.find('.demo-reload').on('click', function() {
							uploadInst.upload();
						});
					}

				});
				//
				//获得年级
				let gradeajax = $.get(window.BASEPATH + "findGrades.do", function(res) {
					if (!res.success) {
						layer.msg(res.msg, {
							icon: 2,
							time: 3000
						});
						return;
					}
					let gid = $("#gradeid");
					$.each(res.data, function(i, o) {
						gid.append(`<option value="${o.id}">${o.gname}</option>`);
					});
					form.render("select");
				});

				//
				//获得学号
				let stuno = window.location.search.substring(1);
				//获得学生信息
				$.when(gradeajax).done(function() {
					$.get(window.BASEPATH + "findStu.do", {
						stuno: stuno
					}, function(res) {
						if (!res.success) {
							layer.msg(result.msg, {
								icon: 2,
								time: 2000
							}, function() {
								parent.layer.close(parent.layer.getFrameIndex(window
									.name)); //关闭当前页

							});
							return;

						}
						//填充学生信息
						let stu = res.data;
						//$("input[name=stuno]").val(stu.stuno);
						for (let k in stu) {
							if (k == "sex") {
								$(`[name=${k}][value='${stu[k]}']`).prop("checked", true);
								continue;
							}
							if (k == "idcardpic") {
								if (stu.idcardpic.indexOf("http") == 0)
									$("#preview").attr("src", stu.idcardpic);
								else
									$("#preview").attr("src", window.BASEPATH + stu.idcardpic);
								continue;
							}
							$(`[name=${k}]`).val(stu[k]);
						}
						form.render();
					});
				});
				//
				form.on('submit(user-save)', function(data) {
					data.field.stuno = stuno; //!!主键
					$.ajax({
						url: window.BASEPATH + 'updateStudent.do',
						data: data.field,
						dataType: 'json',
						type: 'post',
						success: function(result) {
							if (result.success) {
								layer.msg(result.msg, {
									icon: 1,
									time: 1000
								}, function() {
									parent.layer.close(parent.layer.getFrameIndex(window
										.name)); //关闭当前页
									parent.layui.table.reload("role-table");
								});
							} else {
								layer.msg(result.msg, {
									icon: 2,
									time: 3000
								});
							}
						}
					})
					return false;
				});
			})
		</script>
		<script>
		</script>
	</body>
</html>
